package com.mes.service.manu;

import com.yy.base.common.dao.Dao;
import com.yy.base.common.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class KnittingNeeService extends BaseService {
	@Autowired
	private Dao dao;
	private String uuid = null;

	/**
	 * 包装盒条码标签信息
	 * @param id
	 * @return
	 */
	public List<Map<String, Object>> getBoxList(String id) {
		String sql = "";
		return dao.query(sql, id);
	}
	/**
	 * 开工操作
	 * @return
	 */
	public List<Map<String, Object>> getHistoryList(String rfid) {
		String sql = " select  gx.name as GXNAME , mx.route_no as NUMS,mx.status as MXSTATUS, mx.emeso_job_order_exe_id as EXEID,  " +
				" (select a.emeso_job_order_exe_id from  emeso_job_order_exe a where a.emeso_job_order_id = zy.emeso_job_order_id and a.route_no = ( mx.route_no -10) ) as LASTEXEID,  " +
				" (select a.status from  emeso_job_order_exe a where a.emeso_job_order_id = zy.emeso_job_order_id and a.route_no = ( mx.route_no -10) ) as LASTSTATUS  " +
				" from emeso_job_order zy ,emeso_job_order_exe mx , emes_process gx   " +
				" where zy.emeso_job_order_id = mx.emeso_job_order_id  " +
				" and mx.emes_process_id = gx.emes_process_id  " +
				" AND gx.flying_list = 'Y' " +
				" and mx.status = 'CREATE'  " +
				" and zy.code = '"+rfid+"'  " +
				" order by mx.route_no";
		return dao.query(sql);
	}


	//查询该工单对应的针车执行记录
	public List<Map<String, Object>> getjobExeStatusList(String rfid) {
		String sql = "SELECT  " +
				" zy.code as ZYCODE ," +
				"  gx.route_no GXNUM,  " +
				"  gx.NAME GXNAME,  " +
				"  gx.description REMARK,  " +
				"  mx.STATUS MXSTATUS,  " +
				"  eq.NAME AS EQNAME,  " +
				"  mx.emeso_job_order_exe_id EXEID,  " +
				"  wl.NAME AS PRODUCT,  " +
				"  jh.color AS YSNAME,  " +
				"  sc.foot_size_code as CMCODE,  " +
				"  now( ) AS TIMES   " +
				"FROM  " +
				"  Emeso_Job_Order zy,  " +
				"  m_product wl,  " +
				"  emeso_manuorder sc,  " +
				"  emesp_planorder jh,  " +
				"  emes_process gx,  " +
				"  emeso_job_order_exe mx  " +
				"  LEFT JOIN eam_facilities eq ON mx.eam_facilities_id = eq.eam_facilities_id   " +
				"WHERE  " +
				"  zy.emeso_job_order_id = mx.emeso_job_order_id   " +
				"  AND zy.emeso_manuorder_id = sc.emeso_manuorder_id   " +
				"  AND sc.emesp_planorder_id = jh.emesp_planorder_id   " +
				"  AND zy.m_product_id = wl.m_product_id   " +
				"  AND mx.emes_process_id = gx.emes_process_id   " +
				"  AND gx.flying_list = 'Y'   AND zy.CODE = '"+ rfid +"' " +
				" order by gx.route_no ";

		return dao.query(sql);
	}

	//修改工序的执行状态开工
	@SuppressWarnings("unchecked")
	public boolean UpdateExeBeginStatus(String Ids,  String status) {
		boolean result = false;


		String sql =  "UPDATE Emeso_Job_Order_Exe SET status = ?, Exe_Date_From = now()  WHERE  Emeso_Job_Order_Exe_id = ? ";

		result = dao.executeSql(sql, status,Ids) > 0;

		return result;
	}

	//修改工序的执行状态为完工
	@SuppressWarnings("unchecked")
	public boolean UpdateExeEndStatus(String Ids ,String status) {
		boolean result = false;


		String sql =  "UPDATE Emeso_Job_Order_Exe SET status = ?, Exe_Date_To =  now()  WHERE  Emeso_Job_Order_Exe_id = ? ";

		result = dao.executeSql(sql, status,Ids) > 0;

		return result;
	}

	//修改工序的执行状态为暂停
	@SuppressWarnings("unchecked")
	public boolean UpdateExeEndStopStatus(String Ids ,String status) {
		boolean result = false;


		String sql =  "UPDATE Emeso_Job_Order_Exe SET status = ?, Planed_Date_To =  now()  WHERE  Emeso_Job_Order_Exe_id = ? ";

		result = dao.executeSql(sql, status,Ids) > 0;

		return result;
	}

	/**
	 * 根据作业工单号，和工艺id获取作业指导书
	 */
	public String getSopFileId(String orderCode, String processOprId) {
		String sql = "select p.workroute from emes_process_route pr, emes_process p, emeso_job_order jo\n" +
				" where pr.emes_process_route_id = p.emes_process_route_id\n" +
				"   and pr.m_product_id = jo.m_product_id\n" +
				"	and nvl(p.workroute, '') <> ''" +
				"\t and p.emes_processopr_id = ?\n" +
				"\t and jo.code = ?";
		Map<String, Object> result = dao.queryUnique(sql, processOprId, orderCode);
		if (result != null) {
			return (String)result.get("workroute");
		}
		return null;
	}


}